package com.wandian.weather.tools.wnl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;


import com.mylib.libcore.utils.ScreenUtil;
import com.wandian.weather.R;

import java.util.ArrayList;
import java.util.List;

public class SideBar extends View {

    private static final String TAG = "SideBar";
    // 触摸事件
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
    private int choose = 0;
    private Paint paint = new Paint();

    float size = 0f;
    // 天数字体大小
    private int mDayTextSize = 11;
    private DisplayMetrics mMetrics;
    private float height = 0;
    private float fontCount = 2;
    private float strokeWidth = 0;
    private int singleHeight = 0;
    private List<String> stringList = new ArrayList<>();
    Context mContext;
    int color = Color.parseColor("#FFFEFE");
    private Bitmap b1 , b2;

    public void setData(List<String> strings) {
        stringList = strings;
    }

    public SideBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;

        b1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.details_bg_oval);
        b2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.details_bg_oval_pre);
    }

    public SideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        b1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.details_bg_oval);
        b2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.details_bg_oval_pre);
    }

    public SideBar(Context context) {
        super(context);
        this.mContext = context;

        b1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.details_bg_oval);
        b2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.details_bg_oval_pre);
    }

    public void setChoose(int choose) {
        this.choose = choose;
    }

    /**
     * * 重写这个方法
     */
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 获取焦点改变背景颜色.
        int width = getWidth(); // 获取对应宽度
        if (size == 0) {
            paint.setAntiAlias(true);
            // 获取手机屏幕参数
            mMetrics = getResources().getDisplayMetrics();
            paint.setTextSize(ScreenUtil.sp2px(mContext,mDayTextSize));
            size = paint.getTextSize();
            strokeWidth = paint.getStrokeWidth();
        }
        fontCount = 2;
        Paint.FontMetrics fm = paint.getFontMetrics();
        singleHeight = (int) (fm.bottom - fm.top);

        for (int i = 0; i < stringList.size(); i++) {
            int count = stringList.get(i).toCharArray().length;
            RectF r2 = new RectF();                           //RectF对象
            r2.left = ScreenUtil.dip2px(mContext,7);
            r2.top = singleHeight * fontCount + singleHeight / 2;//上边
            r2.right = ScreenUtil.dip2px(mContext,23);                                  //右边
            fontCount += count;
            r2.bottom = r2.top + singleHeight * count + singleHeight - 10;
            paint.setColor(color);
            // 选中的状态
            if (i == choose) {
                paint.setStyle(Paint.Style.FILL);
                canvas.drawBitmap(b2, r2.left, r2.top, new Paint());
            } else {
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(ScreenUtil.dip2px(mContext,1));
                canvas.drawBitmap(b1, r2.left, r2.top, new Paint());
            }
//            canvas.drawRoundRect(r2, 15, 15, paint);

            paint.setStrokeWidth(strokeWidth);

            // 选中的状态
            if (i == choose) {
                paint.setColor(Color.parseColor("#FFFEFE"));
            } else {
                paint.setColor(color);
            }
            // x坐标等于中间-字符串宽度的一半.
            float xPos = (width - paint.measureText("" + stringList.get(i).toCharArray()[0])) / 2+ScreenUtil.dip2px(mContext, 2);
            float yPos = r2.top + singleHeight- ScreenUtil.dip2px(mContext, 3);

            float[] pos = new float[count * 2];
            for (int j = 0; j < (count * 2 - 1); j += 2) {
                pos[j] = xPos;
                pos[j + 1] = yPos + singleHeight * (j / 2) + 10;
                height = pos[j + 1];
            }
            canvas.drawPosText(stringList.get(i), pos, paint);//两个构造函数
            fontCount += 1.2;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();// 点击y坐标
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) (y / (height) * stringList.size());// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.

        switch (action) {
            case MotionEvent.ACTION_UP:
                invalidate();
            default:
                if (oldChoose != c) {
                    if (c >= 0 && c < stringList.size()) {
                        if (listener != null) {
                            listener.onTouchingLetterChanged(c);
                        }
                        choose = c;
                        invalidate();
                    }
                }
                break;
        }
        return true;
    }

    /**
     * 向外公开的方法
     *
     * @param onTouchingLetterChangedListener
     */
    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    /**
     * 接口
     *
     * @author coder
     */
    public interface OnTouchingLetterChangedListener {
        public void onTouchingLetterChanged(int i);
    }

}
